package top.sdaily.business.controller.engineer;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import top.sdaily.business.dto.DEngineer;
import top.sdaily.business.model.engineer.Engineer;
import top.sdaily.business.service.engineer.EngineerService;
import top.sdaily.core.mybatis.Page;
import top.sdaily.storage.Storage;
import top.sdaily.storage.StorageManager;
import top.sdaily.web.context.ReturnBody;

/**
 * Created by soya on 2017/2/9.
 */
@RestController
@RequestMapping("engineer")
public class EngineerController {

    @Autowired
    EngineerService engineerService;
    @Autowired
    StorageManager storageManager;

    @GetMapping("page")
    public ReturnBody getByPage(Page page){
        List<Engineer> engineers = engineerService.getPage(page);
        return ReturnBody.success().setData(engineers).setPage(page);
    }

    @GetMapping("{id}")
    public ReturnBody getById(@PathVariable("id") String id){
        engineerService.findById(id);
        return ReturnBody.success().setData(engineerService.getWithTagsById(id));
    }

    @PutMapping
    public ReturnBody modify(@RequestBody DEngineer engineer){
        engineerService.modify(engineer);
        return ReturnBody.success().setData(engineer);
    }

    @PostMapping
    public ReturnBody create(@RequestBody DEngineer engineer){
        engineerService.create(engineer);
        return ReturnBody.success().setData(engineer);
    }

    @PostMapping("file")
    public ReturnBody uploadFile(@RequestParam("file") MultipartFile file){
        Storage storage = new Storage();
        storage.setFile(file);
        storageManager.storage(storage);
        return ReturnBody.success();
    }
}
